package com.apex.sdk.ui;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.Toolbar;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.model.DailyHeart;
import com.apex.bluetooth.model.DailySport;
import com.apex.bluetooth.model.MultiData;
import com.apex.bluetooth.model.RestingHeart;
import com.apex.bluetooth.model.Sleep;
import com.apex.bluetooth.model.Spo2Data;
import com.apex.bluetooth.model.StressData;
import com.apex.bluetooth.utils.LogUtils;
import com.apex.sdk.R;
import com.apex.sdk.databinding.ActivityDbBinding;
import com.apex.sdk.dialog.WaitingDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


public class DBActivity extends AppCompatActivity {
    private final String TAG = this.getClass().getSimpleName();
    private WaitingDialog waitingDialog;
    List<DailySport> sportList;
    List<DailyHeart> heartList;
    List<MultiData> multiDataList;
    List<Spo2Data> spo2DataList;
    List<StressData> stressDataList;
    List<RestingHeart> restingHeartList;
    List<Sleep> sleepList;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x40) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (sportList != null && !sportList.isEmpty()) {
                    String dailyString = JSONArray.toJSONString(sportList);
                    activityDbBinding.dailyData.setText(dailyString);
                } else {
                    activityDbBinding.dailyData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x41) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.dailyData.setText(null);
                sportList = null;
            } else if (msg.what == 0x42) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (heartList != null && !heartList.isEmpty()) {
                    String heartString = JSONArray.toJSONString(heartList);
                    activityDbBinding.heartData.setText(heartString);
                } else {
                    activityDbBinding.heartData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x43) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.heartData.setText(null);
                heartList = null;
            } else if (msg.what == 0x44) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (multiDataList != null && !multiDataList.isEmpty()) {
                    String heartString = JSONArray.toJSONString(multiDataList);
                    activityDbBinding.multiData.setText(heartString);
                } else {
                    activityDbBinding.multiData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x45) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.multiData.setText(null);
                multiDataList = null;
            } else if (msg.what == 0x46) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (spo2DataList != null && !spo2DataList.isEmpty()) {
                    String heartString = JSONArray.toJSONString(spo2DataList);
                    Log.e(TAG, "查询出来的血氧数据:" + heartString);
                    activityDbBinding.bloodData.setText(heartString);
                } else {
                    activityDbBinding.bloodData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x47) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.bloodData.setText(null);
                spo2DataList = null;
            } else if (msg.what == 0x48) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (stressDataList != null && !stressDataList.isEmpty()) {
                    String heartString = JSONArray.toJSONString(stressDataList);
                    Log.e(TAG, "查询出来的压力数据:" + heartString);
                    activityDbBinding.stressData.setText(heartString);
                } else {
                    activityDbBinding.stressData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x49) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.stressData.setText(null);
                stressDataList = null;
            } else if (msg.what == 0x4A) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (restingHeartList != null && !restingHeartList.isEmpty()) {
                    String heartString = JSONArray.toJSONString(restingHeartList);
                    activityDbBinding.restingData.setText(heartString);
                } else {
                    activityDbBinding.restingData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x4B) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.restingData.setText(null);
                restingHeartList = null;
            } else if (msg.what == 0x4C) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
            } else if (msg.what == 0x4D) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                if (sleepList != null && !sleepList.isEmpty()) {
                    String heartString = JSONArray.toJSONString(sleepList);
                    LogUtils.e(TAG, "查询出来的睡眠数据:" + heartString);
                    activityDbBinding.sleepData.setText(heartString);
                } else {
                    activityDbBinding.sleepData.setText(getString(R.string.no_data));
                }
            } else if (msg.what == 0x4E) {
                if (waitingDialog != null && waitingDialog.isShowing()) {
                    waitingDialog.dismiss();
                }
                activityDbBinding.sleepData.setText(null);
                sleepList = null;
            }
        }
    };
    ActivityDbBinding activityDbBinding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityDbBinding = ActivityDbBinding.inflate(getLayoutInflater());
        setContentView(activityDbBinding.getRoot());
        activityDbBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityDbBinding.tool);
        getSupportActionBar().setTitle(null);
        activityDbBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activityDbBinding.queryDaily.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        sportList = EABleManager.getInstance().queryDailyData(calendar.getTimeInMillis());

                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x40);
                        }

                    }
                }.start();
            }
        });
        activityDbBinding.deleteDaily.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sportList != null && !sportList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < sportList.size(); i++) {
                                keyList.add(sportList.get(i).getRecordDate());
                            }
                            EABleManager.getInstance().deleteDailyData(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x41);
                            }

                        }
                    }.start();
                }
            }
        });
        activityDbBinding.queryHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        heartList = EABleManager.getInstance().queryHeartData(calendar.getTimeInMillis());
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x42);
                        }

                    }
                }.start();
            }
        });
        activityDbBinding.deleteHeart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (heartList != null && !heartList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < heartList.size(); i++) {
                                keyList.add(heartList.get(i).getRecordDate());
                            }
                            EABleManager.getInstance().deleteHeartData(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x43);
                            }

                        }
                    }.start();
                }
            }
        });
        activityDbBinding.queryMulti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        multiDataList = EABleManager.getInstance().queryMultiData(calendar.getTimeInMillis());
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x44);
                        }

                    }
                }.start();
            }
        });
        activityDbBinding.deleteMulti.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (multiDataList != null && !multiDataList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < multiDataList.size(); i++) {
                                keyList.add(multiDataList.get(i).getStartTime());
                            }
                            EABleManager.getInstance().deleteMultiData(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x45);
                            }

                        }
                    }.start();
                }
            }
        });
        activityDbBinding.queryBlood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        spo2DataList = EABleManager.getInstance().querySpo2Data(calendar.getTimeInMillis());
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x46);
                        }

                    }
                }.start();
            }
        });
        activityDbBinding.deleteBlood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (spo2DataList != null && !spo2DataList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < spo2DataList.size(); i++) {
                                keyList.add(spo2DataList.get(i).getRecordDate());
                            }
                            EABleManager.getInstance().deleteSpo2Data(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x47);
                            }

                        }
                    }.start();
                }
            }
        });
        activityDbBinding.queryStress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        stressDataList = EABleManager.getInstance().queryStressData(calendar.getTimeInMillis());
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x48);
                        }

                    }
                }.start();
            }
        });
        activityDbBinding.deleteStress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (stressDataList != null && !stressDataList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < stressDataList.size(); i++) {
                                keyList.add(stressDataList.get(i).getRecordDate());
                            }
                            EABleManager.getInstance().deleteStressData(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x49);
                            }

                        }
                    }.start();
                }
            }
        });
        activityDbBinding.queryResting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        restingHeartList = EABleManager.getInstance().queryRestingHeartData(calendar.getTimeInMillis());
                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x4A);
                        }

                    }
                }.start();
            }
        });
        activityDbBinding.deleteResting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (restingHeartList != null && !restingHeartList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < restingHeartList.size(); i++) {
                                keyList.add(restingHeartList.get(i).getTime());
                            }
                            EABleManager.getInstance().deleteRestingHeartData(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x4B);
                            }

                        }
                    }.start();
                }
            }
        });
        activityDbBinding.addResting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        activityDbBinding.clearData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                EABleManager.getInstance().deleteAllData();
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(0x4C);
                }
            }
        });
        activityDbBinding.clearPointData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                EABleManager.getInstance().deleteRestingHeartData(null);
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(0x4C);
                }
            }
        });
        activityDbBinding.querySleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (waitingDialog == null) {
                    waitingDialog = new WaitingDialog(DBActivity.this);
                }
                if (!waitingDialog.isShowing()) {
                    waitingDialog.show();
                }
                new Thread() {
                    @Override
                    public void run() {
                        super.run();
                        Calendar calendar = Calendar.getInstance();
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
                        sleepList = EABleManager.getInstance().querySleepData(calendar.getTimeInMillis());

                        if (mHandler != null) {
                            mHandler.sendEmptyMessage(0x4D);
                        }
                    }
                }.start();

            }
        });
        activityDbBinding.deleteSleep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sleepList != null && !sleepList.isEmpty()) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(DBActivity.this);
                    }
                    if (!waitingDialog.isShowing()) {
                        waitingDialog.show();
                    }
                    new Thread() {
                        @Override
                        public void run() {
                            super.run();
                            List<Long> keyList = new ArrayList<>();
                            for (int i = 0; i < sleepList.size(); i++) {
                                keyList.add(sleepList.get(i).getStartTime());
                            }
                            EABleManager.getInstance().deleteSleepData(keyList);
                            if (mHandler != null) {
                                mHandler.sendEmptyMessage(0x4E);
                            }

                        }
                    }.start();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        if (waitingDialog != null) {
            waitingDialog.dismiss();
            waitingDialog = null;
        }
        super.onDestroy();
    }
}
